package com.example.sunming.redisLock;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @date: 2020/12/11
 * @author: sunming
 */
@Slf4j
public class TestRedisLock {

    @Autowired
    private RedisLock redisLock;

    public void useLock() {
        // 定义锁的key
        String lockKey = "camera_update_key";
        String uuid = UuidUtils.uuid();

        // 定义超时时间
        long timeout = 5;
        TimeUnit timeUnit = TimeUnit.SECONDS;

        // 加锁
        boolean lock = redisLock.lock(lockKey, uuid, timeout, timeUnit);
        try {
            if (lock) {
                log.info("执行...");
            } else {
                throw new IllegalStateException("未获取到锁，放弃执行");
            }
        } finally {
            // 在finally里面进行解锁
            redisLock.release(lockKey, uuid);
        }
    }
}
